﻿

$(document).ready(function () {    
    var options = {
        types: ['geocode']
    };
    var autocomplete;
    if (document.getElementById('CityName') != null) {
        autocomplete = new google.maps.places.Autocomplete(document.getElementById('CityName'), options);
    }
    //if (document.getElementById('Location') != null) {
    //    autocomplete = new google.maps.places.Autocomplete(document.getElementById('Location'), options);
    //}

    //var mapOptions = {
    //    center: new google.maps.LatLng(18.5204303, 73.85674369999992),
    //    zoom: 13,
    //    mapTypeId: google.maps.MapTypeId.ROADMAP
    //};
    //var map = new google.maps.Map(document.getElementById('map'),
    //  mapOptions);

    //autocomplete.bindTo('bounds', map);

    //var infowindow = new google.maps.InfoWindow();
    //var marker = new google.maps.Marker({
    //    map: map
    //});
    //var input = document.getElementById('CityName');

    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        //infowindow.close();
        //marker.setVisible(false);
        //input.className = '';
        var place = autocomplete.getPlace();

        if (place.address_components) {
            for (i = 0; i < place.address_components.length ; i++) {
                if (place.address_components[i].types[0] == "country") {
                    $("#CountryName").val(place.address_components[i].long_name);
                }
                if (place.address_components[i].types[0] == "administrative_area_level_1") {
                    $("#StateName").val(place.address_components[i].long_name);
                }
                if (place.address_components[i].types[0] == "locality") {
                    $("#CityName").val(place.address_components[i].long_name);
                }
            }
        }







        //if (!place.geometry) {
        //    // Inform the user that the place was not found and return.
        //    //input.className = 'notfound';
        //    return;
        //}

        // If the place has a geometry, then present it on a map.
        //if (place.geometry.viewport) {
        //    map.fitBounds(place.geometry.viewport);
        //} else {
        //    map.setCenter(place.geometry.location);
        //    map.setZoom(17);  // Why 17? Because it looks good.
        //}

        
        //var image = {
        //    url: place.icon,
        //    size: new google.maps.Size(71, 71),
        //    origin: new google.maps.Point(0, 0),
        //    anchor: new google.maps.Point(17, 34),
        //    scaledSize: new google.maps.Size(35, 35)
        //};
        //marker.setIcon(image);
        //marker.setPosition(place.geometry.location);
        //marker.setVisible(true);

        //var address = '';
        //if (place.address_components) {
        //    address = [
        //      (place.address_components[0] && place.address_components[0].short_name || ''),
        //      (place.address_components[1] && place.address_components[1].short_name || ''),
        //      (place.address_components[2] && place.address_components[2].short_name || '')
        //    ].join(' ');
        //}

        //infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
        //infowindow.open(map, marker);

        lat = place.geometry.location.kb;
        lon = place.geometry.location.lb;

        var cityBounds = new google.maps.LatLngBounds(
             new google.maps.LatLng(lat, lon));

        if (document.getElementById('Location') != null) {
            autocomplete = new google.maps.places.Autocomplete(document.getElementById('Location'), options);
        }

        place = autocomplete.getPlace();

        // If the place has a geometry, then present it on a map.
        if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
        } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17);  // Why 17? Because it looks good.
        }
    });

});